package top.lingkang.finalgateway.admin.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.lingkang.finalgateway.admin.entity.AccessLogEntity;
import top.lingkang.finalgateway.admin.param.AccessLogParam;
import top.lingkang.finalgateway.admin.vo.RateIpVO;
import top.lingkang.mm.annotation.MagicMapper;
import top.lingkang.mm.orm.BaseMapper;

import java.util.Date;
import java.util.List;

/**
 * @author lingkang
 * Created by 2024/5/3
 */
@MagicMapper
public interface AccessLogMapper extends BaseMapper<AccessLogEntity> {

    List<AccessLogEntity> list(AccessLogParam param);

    List<AccessLogEntity> getLastLine(@Param("line") int line);

    int deleteIn(@Param("ids") List<String> ids);

    @Select("select create_time from t_access_log where port=#{port} order by create_time desc limit 1")
    Date getLastTime(@Param("port") Integer port);

    @Select("select count(*) from t_access_log where port=#{port} and create_time>=#{time}")
    long countGreaterTime(@Param("port") Integer port, @Param("time") Date time);

    @Select("select ip,count(ip) as number from t_access_log where port=#{port} and create_time>=#{time} group by ip having count(ip)>${rate}")
    List<RateIpVO> getGreaterRateIp(@Param("port") Integer port, @Param("time") Date time, @Param("rate") long rate);

    default void add(String type, String method, String ip, String source, String target, Long size) {
        AccessLogEntity entity = new AccessLogEntity();
        entity.setType(type);
        entity.setMethod(method);
        entity.setIp(ip);
        entity.setSource(source);
        entity.setTarget(target);
        entity.setSize(size);
        insert(entity);
    }
}
